<template>
  <view>
    <!-- 预留出的搜索区域 -->
    <view class="search">
      <MySearch bgcolor="white">
        <text>搜索商品</text>
      </MySearch>
    </view>

    <view class="scroll-content">
      <!-- 左侧的滚动区域 -->
      <scroll-view class="left-scroll" scroll-y :style="{ height: wh + 'px' }">
        <block v-for="(item1, index) in categoryList" :key="index">
          <view
            :class="{ active: selectedCategory === index }"
            @click="clickFirst(index)"
            class="left-scroll-item"
            >{{ item1.name }}</view
          >
        </block>
      </scroll-view>

      <!-- 右侧的滚动区域 -->
      <scroll-view
        class="right-scroll"
        scroll-y
        :style="{ height: wh + 'px' }"
        :scroll-top="scrollTop"
      >
          <!-- 动态渲染二级分类的列表数据 -->
        <view class="cate-list">
            <view
              class="cate-lv2-item"
              v-for="item2 in categoryList[selectedCategory].children"
              :key="item2.category_id"
              @click="jumpList(item2.category_id)"
            >
              <image :src="item2.image.external_url"></image>
              <text>{{ item2.name }}</text>
            </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>
<script>
import MySearch from "@/components/my-serach";
export default {
  components: {
    MySearch,
  },
  data() {
    return {
      // 1. 分类数据数组
      categoryList: [],
      //   控制选中样式
      selectedCategory: 0,
    };
  },
  created() {
    // 2. 调用获取分类数据的方法
    this.getCategoryList();
  },

  methods: {
    // 3. 定义获取分类数据的方法
    async getCategoryList() {
      const { status, data } = await this.$http.get("/category/list");
      console.log(status, data);

      // 请求失败处理
      if (status !== 200) {
        return this.$msg();
      }

      this.categoryList = data.list;
    },
    clickFirst(num) {
      this.selectedCategory = num;
    },
    jumpList(id) {
        wx.navigateTo({
            url: '/pages_sub1/product-list/product-list?categoryId=' + id
        })
    }
  },
};
</script>
<style lang='scss'>
.search {
  position: sticky;
  top: 0px;
  z-index: 999;
}
.my-search-box {
  background-color: #f7f7f7 !important;
}
.scroll-content {
  display: flex;

  .left-scroll {
    width: 170rpx;

    .left-scroll-item {
      height: 90rpx;
      line-height: 120rpx;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 24rpx;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: " ";
          display: block;
          width: 6rpx;
          height: 60rpx;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
  .right-scroll {
    width: 580rpx;
    .cate-list {
        display: flex;
        flex-wrap: wrap;
        align-items:flex-start;
        .cate-lv2-item {
            width: 33.33%;
            height: 230rpx;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 15rpx;
            image {
                width: 150rpx;
                height: 150rpx;
            }
            text {
                width: 100%;
                height: 64rpx;
                text-align: center;
            }
        }
    }
  }
}

</style>